#ifndef _BMS_H
 #define _BMS_H
 
#include "i2cmaster.h"
#include "main.h"
 
#define HELLOALL	0b11100000
#define WRITEALL	0b10100000
#define BROADCAST	0x40
#define ROLLALL		0x00
#define CELLSCANEN	0x09
#define SCANCTRL	0x0D
#define	CELLREGBASE	0x20
#define ADCCFG		0x08
#define BALCFG		0x0B 
#define GPIO		0x0A
#define	ACQCFG		0x0C

#define OVSAMPL 	0b011	//3 bit value (001 = oversampling 4; 011 = oversampling 16; 111 = oversampling 128)

// 1bit = 1mV -> Ausgabe normiert auf 32768mV / 4 = 8192mV 
// Full Range = 5000mV = 4096 (ADC) -> Gain = 5000/8192 * 32768/4096 = 4.883e-3 -> normiert: 4.883/4 * 32768 = 40000
#define BMS_VOLTAGE_GAIN 40000
#define SHIFT_BMS_VOLTAGE 2 //(for display: >> SHIFT_BMS_VOLTAGE)

#define PORT_BMS_VCC PORTC
#define DDR_BMS_VCC	DDRC
#define	BMS_VCC		(1<<1)


extern unsigned int cellVoltage[12];

extern unsigned char PEC(unsigned char *buf, unsigned char length);
extern void bmsStartConversion(unsigned char waitDone);		
extern unsigned int bmsReadCell(unsigned char cell);	
extern void bmsReadAll(void);	
extern void bmsBalanceCell(unsigned int balCFG, unsigned char timeout); 			
extern void bmsSetChargeEnable(unsigned char enable); 		
extern void bmsEnable(unsigned char enable);				
extern unsigned int bmsBalanceAll(unsigned int tresholdVoltage, unsigned int maxVoltage, unsigned char timeout);
extern unsigned char bmsReady(void);

 
#endif